<?php


namespace App\backend\controllers\goods;


use App\backend\controllers\AdminBaseController;
use App\backend\models\CategoryModel;
use App\backend\services\CategoryServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Utils\JsonResp;

class CategoryController extends AdminBaseController
{
    protected $class_name = 'Category';

    /**
     * 商品分类列表
     */
    public function getList(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            CategoryModel::rules(),
            CategoryModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = CategoryServices::getList($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 添加/编辑商品分类
     */
    public function categoryModify(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            CategoryModel::modify_rules(),
            CategoryModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = CategoryServices::categoryModify($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 显示/隐藏商品分类
     */
    public function categoryEnabled(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            CategoryModel::enabled_rules(),
            CategoryModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = CategoryServices::updateCategoryEnabled($data);
        return JsonResp::success('成功', $list);
    }

    /**
     * 下架/删除商品分类
     */
    public function categoryDelete(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            CategoryModel::updated_rules(),
            CategoryModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return JsonResp::error($msg[0]);
        }
        $list = CategoryServices::updateCategoryStatus($data);
        return JsonResp::success('成功', $list);
    }

}
